<?php
/**
 * 订单数据验证器
 */
declare(strict_types=1);

namespace app\validate\crm;

use think\Validate;

class OrderValidate extends Validate
{
    protected $rule = [
        'customer_id'       => 'require|integer|gt:0',
        'customer_type'     => 'require|in:1,2',
        'order_category'    => 'require|in:1,2',
        'order_type'        => 'require|integer|gt:0',
        'contract_amount'   => 'require|float|egt:0',
        'post_amount'       => 'float|egt:0',
        'contract_no'       => 'max:50',
        'contract_sign_date'=> 'integer',
        'contact_id'        => 'integer',
        'payment_records'   => 'array',
    ];
    
    protected $message = [
        'customer_id.require'       => '客户ID不能为空',
        'customer_id.integer'       => '客户ID必须为整数',
        'customer_id.gt'            => '客户ID必须大于0',
        'customer_type.require'     => '客户类型不能为空',
        'customer_type.in'          => '客户类型必须为1(个人)或2(企业)',
        'order_category.require'    => '订单大类不能为空',
        'order_category.in'         => '订单大类必须为1(诉讼)或2(非诉)',
        'order_type.require'        => '订单类型不能为空',
        'contract_amount.require'   => '合同金额不能为空',
        'contract_amount.float'     => '合同金额必须为数字',
        'contract_amount.egt'       => '合同金额不能为负数',
        'post_amount.float'         => '后期费用必须为数字',
        'post_amount.egt'           => '后期费用不能为负数',
    ];
    
    protected $scene = [
        'create' => ['customer_id', 'customer_type', 'order_category', 'order_type', 'contract_amount', 'post_amount'],
        'update' => ['contract_amount', 'post_amount'],
    ];
    
    /**
     * 创建订单场景验证
     */
    public function sceneCreate()
    {
        return $this->only(['customer_id', 'customer_type', 'order_category', 'order_type', 'contract_amount', 'post_amount'])
            ->append('order_category', function ($value, $data) {
                // 诉讼订单需要验证诉讼详情
                if ($value == 1 && empty($data['lawsuit_detail'])) {
                    return '诉讼订单必须填写诉讼详情';
                }
                
                // 非诉订单需要验证非诉详情
                if ($value == 2 && empty($data['legal_service_detail'])) {
                    return '非诉订单必须填写非诉详情';
                }
                
                return true;
            });
    }
}
